<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) 2023 https://inphp.cc All rights reserved.
// | Licensed ( https://opensource.org/licenses/MIT )
// | Author: 幺月儿(https://gitee.com/lulanyin) Email: inphp@qq.com
// +----------------------------------------------------------------------
// | 分组/权限设置
// +----------------------------------------------------------------------
namespace app\sso\http\api\admin;

use app\sso\model\UserModel;
use Inphp\Core\Db\Db;
use Inphp\Core\Object\Message;
use Inphp\Core\Util\Str;

#[\app\admin\attributes\auth(null, true)] class group
{
    /**
     * 获取分组详情，包含详细的权限
     * @return Message
     */
    public function index(): Message
    {
        $id = GET("id");
        $id = is_numeric($id) && $id > 0 ? ceil($id) : 0;
        if ($id <= 0) {
            return httpMessage(0, "参数无效");
        }
        $group = Db::from("sso_group")->where("id", $id)->first();
        if (empty($group)) {
            return httpMessage("未找到数据");
        }
        $group["permission"] = !empty($group["permission"]) ? @json_decode($group["permission"]) : [];
        return httpMessage($group);
    }

    /**
     * 获取分组列表
     * @return array
     */
    public function list(): array
    {
        $list = Db::from("sso_group")->get();
        foreach ($list as &$item) {
            $item["users"] = UserModel::emptyQuery()->whereFindInSet($item["id"], "`groups`")->rows();
        }
        $detail = GET("detail");
        if ($detail == 1) {
            //处理获得详情

        }
        return $list;
    }

    /**
     * 保存
     * @return Message
     */
    public function save(): Message
    {
        $name = POST("name");
        $name = !empty($name) ? Str::trim($name) : null;
        if (empty($name)) {
            return httpMessage("缺少标签名称");
        }
        if (mb_strlen($name) > 20) {
            return httpMessage("名称请控制在20个字符内");
        }

        $desc = POST("desc");
        $desc = !empty($desc) ? trim($desc) : null;

        $id = POST("id");
        $id = is_numeric($id) && $id > 0 ? ceil($id) : 0;

        //检测同名
        $exists = Db::from("sso_group")
            ->where("name", $name)
            ->where("id", "!=", $id)
            ->first();
        if (!empty($exists)) {
            return httpMessage("已存在相同名称的分组");
        }
        //权限
        $permission = POST("permission");
        $permission = !empty($permission) ? $permission : null;
        if (!empty($permission)) {
            //保存权限情况
            $permission = is_array($permission) ? json_encode($permission) : $permission;
        }
        $data = [
            "name"      => $name
        ];
        if (!is_null($desc)) {
            $data["desc"] = $desc;
        }
        if (!empty($permission)) {
            $data["permission"] = $permission;
        }
        //可以保存
        $db = Db::from("sso_group");
        $execute = "insert";
        if ($id > 0) {
            $db->where("id", $id);
            $execute = "update";
        }
        if ($db->{$execute}($data)) {
            return httpMessage(0, "保存成功", [
                "id"    => $id > 0 ? $id : $db->getLastInsertId()
            ]);
        }
        return httpMessage("保存失败");
    }

    /**
     * 删除单个分组
     * @return Message
     */
    public function delete(): Message
    {
        $id = POST("id");
        $id = is_numeric($id) && $id > 0 ? ceil($id) : 0;
        if ($id <= 0) {
            return httpMessage("ID参数无效");
        }

        $db = Db::from("sso_group")->where("id", $id);
        if ($db->delete()) {
            return httpMessage(0, "已删除{$db->getAffectRows()}个分组");
        }
        return httpMessage("删除操作失败");
    }
}